package etxt2db.annotators;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import edu.cmu.minorthird.text.Annotator;
import edu.cmu.minorthird.text.BasicTextLabels;
import edu.cmu.minorthird.text.MonotonicTextLabels;
import edu.cmu.minorthird.text.Span;
import edu.cmu.minorthird.text.TextLabels;
import etxt2db.api.ClassificationModel;


public class HighPrecisionAnnotator  extends ClassificationModel implements Serializable {
	List<Annotator> annotators = new ArrayList<Annotator>();
	
	public void addAnnotator(Annotator an){
		annotators.add(an);
	}
	
	public void annotate(MonotonicTextLabels arg0) {
		List<Span> listSpans = new ArrayList<Span>();
		boolean isFirst = true;
		for(Annotator an : annotators){
			if(isFirst){
				TextLabels temp = an.annotatedCopy(arg0);
				Iterator<Span> iter = temp.getTextBase().documentSpanIterator();
				while(iter.hasNext()){
					Span span = iter.next();
					for(Span text : temp.getTypeSet("indicacao", span.getDocumentId())){
						listSpans.add(text);
					}
				}
			}else{
				TextLabels temp = an.annotatedCopy(arg0);
				List<Span> listSpansCurrent = new ArrayList<Span>();
				Iterator<Span> iter = temp.getTextBase().documentSpanIterator();
				while(iter.hasNext()){
					Span span = iter.next();
					for(Span text : temp.getTypeSet("indicacao", span.getDocumentId())){
						listSpansCurrent.add(text);
					}
				}
				List<Span> listSpansRemoval = new ArrayList<Span>();
				for(Span span : listSpans){
					if(!listSpansCurrent.contains(span)){
						listSpansRemoval.add(span);
					}
				}
				listSpans.removeAll(listSpansRemoval);
			}
			isFirst = false;
		}
		for(Span span : listSpans){
			arg0.addToType(span, "indicacao");
		}
	}

	@Override
	public TextLabels annotatedCopy(TextLabels arg0) {
		BasicTextLabels copy = new BasicTextLabels(arg0.getTextBase());
		annotate(copy);
		//Return the new TextLabels
		return copy;
	}

	@Override
	public String explainAnnotation(TextLabels arg0, Span arg1) {
		//Not important in this work... but necessary to implement the interface
		return "";
	}
}
